package com.adprof.demo.main;

import static com.adprof.demo.utils.ConstantsDemo.LOG_TAG;
import static com.adprof.demo.utils.TimeUtils.getDateTimeFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.adprof.demo.android.R;
import com.adprof.sdk.api.InitCallback;
import com.adprof.demo.banner.BannerDemoActivity;
import com.adprof.demo.interstitial.InterstitialDemoActivity;
import com.adprof.demo.natives.NativeAdDemoActivity;
import com.adprof.demo.reward.RewardDemoActivity;
import com.adprof.demo.splash.SplashDemoActivity;
import com.adprof.demo.utils.ConstantsDemo;
import com.adprof.demo.utils.SDKInitUtil;

import java.util.Date;

public class MainFragment extends Fragment {
    private static final String ARG_KEY_LOGS = "logs";

    public static MainFragment newInstance(String[] logs) {
        MainFragment ret = new MainFragment();
        Bundle arguments = new Bundle();
        arguments.putStringArray(ARG_KEY_LOGS, logs);
        ret.setArguments(arguments);
        return ret;
    }

    private Activity mActivity;
    String splashAdCodeId = ConstantsDemo.SPLASH_SLOTID;

    private Button cleanLogButton;
    private Button interBtn;
    private Button rewardBtn;
    private Button nativeBtn;
    private Button bannerBtn;
    private TextView logTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        cleanLogButton = view.findViewById(R.id.cleanLog_button);
        interBtn = view.findViewById(R.id.interstitial_button);
        rewardBtn = view.findViewById(R.id.reward_button);
        nativeBtn = view.findViewById(R.id.native_button);
        bannerBtn = view.findViewById(R.id.banner_button);
        logTextView = view.findViewById(R.id.logView);

        view.findViewById(R.id.splash_button_load).setOnClickListener(v -> loadSplashAd());

        cleanLogButton.setOnClickListener(v -> cleanLog());
        logTextView.setOnLongClickListener(v -> true);
        logTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

        WebView.setWebContentsDebuggingEnabled(true);

        interBtn.setOnClickListener(v -> {
            Intent intent = new Intent(getMyActivity(), InterstitialDemoActivity.class);
            startActivity(intent);
        });

        rewardBtn.setOnClickListener(v -> {
            Intent intent = new Intent(getMyActivity(), RewardDemoActivity.class);
            startActivity(intent);
        });

        nativeBtn.setOnClickListener(v -> {
            Intent intent = new Intent(getMyActivity(), NativeAdDemoActivity.class);
            startActivity(intent);
        });

        bannerBtn.setOnClickListener(v -> {
            Intent intent = new Intent(getMyActivity(), BannerDemoActivity.class);
            startActivity(intent);
        });

        view.findViewById(R.id.sdk_init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSDK();
            }
        });
    }

    private void loadSplashAd() {
        startActivity(new Intent(getMyActivity(), SplashDemoActivity.class));
        logMessage("start load splash " + splashAdCodeId);
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;//保存Context引用
    }

    private Activity getMyActivity() {
        if (mActivity == null) {
            mActivity = getActivity();
        }
        return mActivity;
    }

    private void cleanLog() {
        logTextView.setText("");
    }

    private void logMessage(String message) {
        Date date = new Date();
        logTextView.append(getDateTimeFormat().format(date) + " " + message + '\n');
    }

    private void initSDK() {
        SDKInitUtil.initSDK(getActivity(), "", new InitCallback() {
            @Override
            public void onSuccess() {
                Log.d(LOG_TAG, "init-------onSuccess-----------");
                logMessage("初始化成功");
            }

            @Override
            public void onFail(int code, String msg) {
                Log.d(LOG_TAG, "init--------------onFail-----------" + code + ":" + msg);
                logMessage("初始化失败" + msg);
            }
        });
    }


}
